package jsmbscanner;
/*
 * GNU GENERAL PUBLIC LICENSE
 * Version 3, 29 June 2007
 *
 * Copyright (C) 2009, vlad
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Toolkit;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import org.jdesktop.application.Action;

/**
 *
 * @author vlad
 */
public class SetSetup extends javax.swing.JDialog {

    static private long setIPStart;
    static private long setIPEnd;
    static private long networkAddress;
    static private String networkAddressText;
    static private String networkClass;
    static private String networkBroadcast;
    static private String hostCount;
    static private String maskCIDRtext;
    // Regular expression for IP addresses and Mask(full format)
    static final Pattern patIP = Pattern.compile("\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
            "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
            "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
            "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b");
    // Regular expression for Mask (CIDR)
    static final Pattern patMask = Pattern.compile("\\b([0-9][0-9])");


    // Check IP
    static boolean checkIP(String s) {
        Matcher m = patIP.matcher(s);
        return m.matches();
    }

    // Check mask
    static boolean checkMask(String s) {
        Matcher m = patMask.matcher(s);
        int val = Integer.decode(s);
        boolean result = false;
        if ((m.matches()) & (val < 33)) {
            result = true;
        }
        return result;

    }

    // Transfer info to main form
    public static long getStartIP() {
        long result = setIPStart;
        return result;
    }

    public static String getStartIPtext() {
        String result="";
        try {
            result = InetTools.NumToAddress(setIPStart);
        } catch (UnknownHostException ex) {
            Logger.getLogger(SetSetup.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    static public long getStopIP() {
        long ipEndNum = setIPEnd;
        return ipEndNum;
    }

    public static String getStopIPtext() {
        String result="";
        try {
            result = InetTools.NumToAddress(setIPEnd);
        } catch (UnknownHostException ex) {
            Logger.getLogger(SetSetup.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    static public String getNetworkClass() {
        return networkClass;
    }

    static public String getNetworkAddress() {
        return networkAddressText;
    }

    static public String getNetworkBroadcast() {
        return networkBroadcast;
    }

    static public String getHostCount() {
        return hostCount;
    }

    static public String getMaskCIDR() {
        return maskCIDRtext;
    }



    private void calcSettings() throws UnknownHostException {
        long bits32 = 0xFFFFFFFFL;
        String ipStart = textFieldIPStart.getText();
        String ipEnd = textFieldIPEnd.getText();
        long ipStartNum = InetTools.AddressToNum(ipStart);
        long ipEndNum = InetTools.AddressToNum(ipEnd);
        // IP range is in manual mode
        if (radioIPEnd.isSelected()) {
            setIPStart = ipStartNum;
            setIPEnd = InetTools.AddressToNum(ipEnd);
            networkAddress =-1;
            networkAddressText = "Manual range";
            networkClass = "~"+InetTools.getNetworkClass(ipStartNum);
            networkBroadcast = "Manual range";
            hostCount = Long.toString(ipEndNum - ipStartNum);
            maskCIDRtext = "Manual range";
            
        } else if (radioCIDR.isSelected()) {
            String maskCIDR = textFieldCIDR.getText();
            // Convert from CIDR form to long number
            long maskNum = InetTools.convertMask(maskCIDR);
            long netAddress = (long) ipStartNum & maskNum;
            long broadAddress = (netAddress | (~maskNum)) & bits32;
            ipStartNum = netAddress + 1;
            ipEndNum = broadAddress - 1;
            setIPStart = ipStartNum;
            setIPEnd = ipEndNum;
            networkAddress = netAddress;
            networkAddressText = InetTools.NumToAddress(networkAddress);
            networkClass = InetTools.getNetworkClass(networkAddress);
            networkBroadcast = InetTools.NumToAddress(broadAddress);
            hostCount = Long.toString(ipEndNum - ipStartNum);
            maskCIDRtext = maskCIDR;
            
        } else if (radioFull.isSelected()) {
            String maskFull = textFieldMaskFull.getText();
            long maskNum = InetTools.AddressToNum(maskFull);
            maskNum = InetTools.AddressToNum(maskFull);
            long netAddress = (long) ipStartNum & maskNum;
            long broadAddress = (netAddress | (~maskNum)) & bits32;
            ipStartNum = netAddress + 1;
            ipEndNum = broadAddress - 1;
            setIPStart = ipStartNum;
            setIPEnd = ipEndNum;
            networkAddress = netAddress;
            networkAddressText = InetTools.NumToAddress(netAddress);
            networkClass = InetTools.getNetworkClass(networkAddress);
            networkBroadcast = InetTools.NumToAddress(broadAddress);
            maskCIDRtext = InetTools.convertMask(maskNum);
            long countHosts = ipEndNum - ipStartNum;
            countHosts++; // Increment only for display
            hostCount = Long.toString(countHosts++);

        }
    }

    /** Creates new form NewJDialog */
    public SetSetup(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        try {
            calcSettings();
        } catch (UnknownHostException ex) {
            Logger.getLogger(SetSetup.class.getName()).log(Level.SEVERE, null, ex);
        }
        // Set listener for "IP start" format field
        textFieldIPStart.getDocument().addDocumentListener(new DocumentListener() {

            void checkDocument(DocumentEvent e) {
                try {
                    String text = e.getDocument().getText(0, e.getDocument().getLength());
                    buttonApply.setEnabled(checkIP(text));
                } catch (BadLocationException ex) {
                    //Do something, OK?
                    System.out.println("We have a error with checking IP start!");
                }
            }

            public void insertUpdate(DocumentEvent e) {
                checkDocument(e);
            }

            public void removeUpdate(DocumentEvent e) {
                checkDocument(e);
            }

            public void changedUpdate(DocumentEvent e) {
                checkDocument(e);
            }
        });
        textFieldIPStart.setInputVerifier(new InputVerifier() {

            @Override
            public boolean shouldYieldFocus(JComponent input) {
                boolean inputOK = verify(input);
                if (inputOK) {
                    return true;
                } else {
                    Toolkit.getDefaultToolkit().beep();
                    return false;
                }
            }

            public boolean verify(JComponent input) {
                JTextField field = (JTextField) input;
                return checkIP(field.getText());
            }
        });
        // *** End


        // Set listener for "IP end" format field
        textFieldIPEnd.getDocument().addDocumentListener(new DocumentListener() {

            void checkDocument(DocumentEvent e) {
                try {
                    String text = e.getDocument().getText(0, e.getDocument().getLength());
                    buttonApply.setEnabled(checkIP(text));
                } catch (BadLocationException ex) {
                    //Do something, OK?
                    System.out.println("We have a error with checking IP end!");
                }
            }

            public void insertUpdate(DocumentEvent e) {
                checkDocument(e);
            }

            public void removeUpdate(DocumentEvent e) {
                checkDocument(e);
            }

            public void changedUpdate(DocumentEvent e) {
                checkDocument(e);
            }
        });
        textFieldIPEnd.setInputVerifier(new InputVerifier() {

            @Override
            public boolean shouldYieldFocus(JComponent input) {
                boolean inputOK = verify(input);
                if (inputOK) {
                    return true;
                } else {
                    Toolkit.getDefaultToolkit().beep();
                    return false;
                }
            }

            public boolean verify(JComponent input) {
                JTextField field = (JTextField) input;
                return checkIP(field.getText());
            }
        });
        // *** End


        // Set listener for "Mask (full format)" field
        textFieldMaskFull.getDocument().addDocumentListener(new DocumentListener() {

            void checkDocument(DocumentEvent e) {
                try {
                    String text = e.getDocument().getText(0, e.getDocument().getLength());
                    buttonApply.setEnabled(checkIP(text));
                } catch (BadLocationException ex) {
                    //Do something, OK?
                    System.out.println("We have a error with checking Mask (FULL)!");
                }
            }

            public void insertUpdate(DocumentEvent e) {
                checkDocument(e);
            }

            public void removeUpdate(DocumentEvent e) {
                checkDocument(e);
            }

            public void changedUpdate(DocumentEvent e) {
                checkDocument(e);
            }
        });
        textFieldMaskFull.setInputVerifier(new InputVerifier() {

            @Override
            public boolean shouldYieldFocus(JComponent input) {
                boolean inputOK = verify(input);
                if (inputOK) {
                    return true;
                } else {
                    Toolkit.getDefaultToolkit().beep();
                    return false;
                }
            }

            public boolean verify(JComponent input) {
                JTextField field = (JTextField) input;
                return checkIP(field.getText());
            }
        });
        // *** End

        // Set listener for "Mask (CIDR)" field
        textFieldCIDR.getDocument().addDocumentListener(new DocumentListener() {

            void checkDocument(DocumentEvent e) {
                try {
                    String text = e.getDocument().getText(0, e.getDocument().getLength());
                    buttonApply.setEnabled(checkMask(text));
                } catch (BadLocationException ex) {
                    //Do something, OK?
                    System.out.println("We have a error with checking Mask (CIDR)!");
                }
            }

            public void insertUpdate(DocumentEvent e) {
                checkDocument(e);
            }

            public void removeUpdate(DocumentEvent e) {
                checkDocument(e);
            }

            public void changedUpdate(DocumentEvent e) {
                checkDocument(e);
            }
        });
        textFieldCIDR.setInputVerifier(new InputVerifier() {

            @Override
            public boolean shouldYieldFocus(JComponent input) {
                boolean inputOK = verify(input);
                if (inputOK) {
                    return true;
                } else {
                    Toolkit.getDefaultToolkit().beep();
                    return false;
                }
            }

            public boolean verify(JComponent input) {
                JTextField field = (JTextField) input;
                return checkMask(field.getText());
            }
        });
    // *** End





    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        buttonGroup1 = new javax.swing.ButtonGroup();
        jSeparator1 = new javax.swing.JSeparator();
        jPanel1 = new javax.swing.JPanel();
        labelIPStart = new javax.swing.JLabel();
        radioCIDR = new javax.swing.JRadioButton();
        radioFull = new javax.swing.JRadioButton();
        radioIPEnd = new javax.swing.JRadioButton();
        textFieldIPStart = new javax.swing.JFormattedTextField();
        textFieldIPEnd = new javax.swing.JFormattedTextField();
        textFieldMaskFull = new javax.swing.JFormattedTextField();
        textFieldCIDR = new javax.swing.JFormattedTextField();
        buttonApply = new javax.swing.JButton();
        buttonCancel = new javax.swing.JButton();
        labelAlarm = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(jsmbscanner.JSmbScannerApp.class).getContext().getResourceMap(SetSetup.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setIconImage(null);
        setModal(true);
        setName("Form"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setName("jPanel1"); // NOI18N

        labelIPStart.setFont(resourceMap.getFont("labelIPStart.font")); // NOI18N
        labelIPStart.setText(resourceMap.getString("labelIPStart.text")); // NOI18N
        labelIPStart.setName("labelIPStart"); // NOI18N

        buttonGroup1.add(radioCIDR);
        radioCIDR.setText(resourceMap.getString("radioCIDR.text")); // NOI18N
        radioCIDR.setName("radioCIDR"); // NOI18N

        buttonGroup1.add(radioFull);
        radioFull.setText(resourceMap.getString("radioFull.text")); // NOI18N
        radioFull.setName("radioFull"); // NOI18N

        buttonGroup1.add(radioIPEnd);
        radioIPEnd.setSelected(true);
        radioIPEnd.setText(resourceMap.getString("radioIPEnd.text")); // NOI18N
        radioIPEnd.setName("radioIPEnd"); // NOI18N

        textFieldIPStart.setText(resourceMap.getString("textFieldIPStart.text")); // NOI18N
        textFieldIPStart.setToolTipText(resourceMap.getString("textFieldIPStart.toolTipText")); // NOI18N
        textFieldIPStart.setName("textFieldIPStart"); // NOI18N

        textFieldIPEnd.setText(resourceMap.getString("textFieldIPEnd.text")); // NOI18N
        textFieldIPEnd.setToolTipText(resourceMap.getString("textFieldIPEnd.toolTipText")); // NOI18N
        textFieldIPEnd.setName("textFieldIPEnd"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, radioIPEnd, org.jdesktop.beansbinding.ELProperty.create("${selected}"), textFieldIPEnd, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        textFieldMaskFull.setText(resourceMap.getString("textFieldMaskFull.text")); // NOI18N
        textFieldMaskFull.setName("textFieldMaskFull"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, radioFull, org.jdesktop.beansbinding.ELProperty.create("${selected}"), textFieldMaskFull, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        textFieldCIDR.setText(resourceMap.getString("textFieldCIDR.text")); // NOI18N
        textFieldCIDR.setName("textFieldCIDR"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, radioCIDR, org.jdesktop.beansbinding.ELProperty.create("${selected}"), textFieldCIDR, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelIPStart)
                    .addComponent(radioIPEnd)
                    .addComponent(radioFull)
                    .addComponent(radioCIDR))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(textFieldIPEnd, 0, 0, Short.MAX_VALUE)
                        .addComponent(textFieldIPStart))
                    .addComponent(textFieldMaskFull, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textFieldCIDR, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {textFieldIPEnd, textFieldIPStart, textFieldMaskFull});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(labelIPStart)
                    .addComponent(textFieldIPStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textFieldCIDR, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(radioCIDR, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(radioFull)
                    .addComponent(textFieldMaskFull, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textFieldIPEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(radioIPEnd))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(jsmbscanner.JSmbScannerApp.class).getContext().getActionMap(SetSetup.class, this);
        buttonApply.setAction(actionMap.get("actionApply")); // NOI18N
        buttonApply.setIcon(resourceMap.getIcon("buttonApply.icon")); // NOI18N
        buttonApply.setMnemonic('a');
        buttonApply.setText(resourceMap.getString("buttonApply.text")); // NOI18N
        buttonApply.setName("buttonApply"); // NOI18N

        buttonCancel.setAction(actionMap.get("actionCancel")); // NOI18N
        buttonCancel.setIcon(resourceMap.getIcon("buttonCancel.icon")); // NOI18N
        buttonCancel.setMnemonic('c');
        buttonCancel.setText(resourceMap.getString("buttonCancel.text")); // NOI18N
        buttonCancel.setName("buttonCancel"); // NOI18N

        labelAlarm.setFont(new java.awt.Font("Dialog", 0, 10));
        labelAlarm.setForeground(resourceMap.getColor("labelAlarm.foreground")); // NOI18N
        labelAlarm.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        labelAlarm.setText(resourceMap.getString("labelAlarm.text")); // NOI18N
        labelAlarm.setName("labelAlarm"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(labelAlarm)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(12, 12, 12))
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addComponent(buttonApply)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE)
                .addComponent(buttonCancel)
                .addGap(34, 34, 34))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(labelAlarm)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 4, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonCancel)
                    .addComponent(buttonApply))
                .addContainerGap(17, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-340)/2, (screenSize.height-230)/2, 340, 230);
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                SetSetup dialog = new SetSetup(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    @Action
    public void actionCancel() {
        // Hide window
        setVisible(false);
    }

    @Action
    public void actionApply() throws UnknownHostException {
        // We need to compare IP addresses, don't we?
        if (radioIPEnd.isSelected()) {
            long ipStart = InetTools.AddressToNum(textFieldIPStart.getText());
            long ipEnd = InetTools.AddressToNum(textFieldIPEnd.getText());
            if (ipStart >= ipEnd) {
                System.out.println("IP ranges error!");
                Toolkit.getDefaultToolkit().beep();
                labelAlarm.setText("Data incorrect! Check your IP range !!!");
                labelAlarm.revalidate();
                labelAlarm.repaint();

                // clear the labelAlarm after delay and from a non-Swing thread.
                SwingUtilities.invokeLater(new Runnable() {

                    public void run() {
                        try {
                            Thread.sleep(3000L); // thread is sleeping
                            labelAlarm.setText("");
                            labelAlarm.revalidate();
                            labelAlarm.repaint();
                        } catch (InterruptedException ex) {
                            Logger.getLogger(SetSetup.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                });

            } else {
                //Everything's okey - set variables & hide window
                calcSettings();
                setVisible(false);
            }
        } else {
            calcSettings();
            setVisible(false);
        }

    }

    public void setButtonAlarm() {
        labelAlarm.setText("Data incorrect! Check your IP range !!! (F)");
        pack();
        repaint();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonApply;
    private javax.swing.JButton buttonCancel;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel labelAlarm;
    private javax.swing.JLabel labelIPStart;
    private javax.swing.JRadioButton radioCIDR;
    private javax.swing.JRadioButton radioFull;
    private javax.swing.JRadioButton radioIPEnd;
    private javax.swing.JFormattedTextField textFieldCIDR;
    private javax.swing.JFormattedTextField textFieldIPEnd;
    private javax.swing.JFormattedTextField textFieldIPStart;
    private javax.swing.JFormattedTextField textFieldMaskFull;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
